﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcDemo.Control
{
   /// <summary>
    /// 控件Builder基类
    /// </summary>
    /// <typeparam name="TControl"></typeparam>
    /// <typeparam name="TBuilder"></typeparam>
    public abstract class MvcControlBuilderBase<TControl, TBuilder>
        where TControl : MvcControlBase
        where TBuilder : MvcControlBuilderBase<TControl, TBuilder>
    {
        protected MvcControlBuilderBase(TControl control)
        {
            if (control == null)
                throw new ArgumentNullException("control");

            this.Control = control;
        }
        /// <summary>
        /// 要生成的控件
        /// </summary>
        public TControl Control { get; private set; }

        /// <summary>
        /// 设置控件名称
        /// </summary>
        /// <param name="controlName">控件名称</param>
        public virtual TBuilder Name(string controlName)
        {
            Control.Name = controlName;
            return this as TBuilder;
        }

        

        ///// <summary>
        ///// 设置要包含的js文件路径
        ///// </summary>
        ///// <param name="names">文件路径</param>
        //public virtual TBuilder ScriptFileNames(params string[] names)
        //{
        //    Control.ScriptFileNames.Clear();
        //    Control.ScriptFileNames.AddRange(names);
        //    return this as TBuilder;
        //}

        ///// <summary>
        ///// 以匿名方式设置控件html属性
        ///// </summary>
        ///// <param name="attributes">html属性集合</param>
        //public virtual TBuilder HtmlAttributes(object attributes)
        //{
        //    Control.HtmlAttributes.Clear();
        //    //TODO:
        //    //Control.HtmlAttributes.Merge(attributes);
        //    return this as TBuilder;
        //}

        /// <summary>
        /// 以Html方式输出控件代码
        /// </summary>
        public virtual IHtmlString Render()
        {
            return Control.Render();
        }

        public override string ToString()
        {
            return Render().ToString();
        }
    }
}